package userPages;
import pages.*;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import templates.Template;
import database.DBUtils;

/**
 * Servlet implementation class ViewInstrument
 */
public class ViewInstrument extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewInstrument() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			Template template = new Template("user_templates\\instrument_view");

			Connection conn = null;
			PreparedStatement selectUserStmt = null;
			ResultSet rs = null;
			try {
				conn = DBUtils.getConnection();
					selectUserStmt = conn
						.prepareStatement("SELECT i.id, i.name as name, it. name as type, i.timeSlot, i.description, i.messageforusers, i.isActive"
								+ " from instruments as i, instrumenttypes as it "
								+ "WHERE i.typeID = it.id && i.id =?;");
				selectUserStmt.setString(1, request.getParameter("id"));
				rs = selectUserStmt.executeQuery();
				if (rs == null || !rs.next()) {
					
					template.addChange("id", "Fail");

				} else {
					
					String desc = rs.getString("description");
					if(desc.isEmpty())
						desc = "none";
					String mess = rs.getString("messageforusers");
					if(mess.isEmpty())
						mess = "none";
					template.addChange("id", rs.getString("name"));
					template.addChange("type", rs.getString("type"));
					template.addChange("description",desc);
						template.addChange("timeslot", rs.getString("timeslot") + " Min");
					if(rs.getString("isActive").compareTo("1") == 0)
						template.addChange("active", "Yes");
					else
						template.addChange("active", "No");
					template.addChange("message", mess);

				}
			} catch (Exception e) {
				
				template.addChange("id", e.getMessage());
			} finally {
				DBUtils.close(rs);
				DBUtils.close(selectUserStmt);
				DBUtils.close(conn);
			}
			response.getOutputStream().print(
					template.createPage(
							"Instruments Labaratory: View Instrument Information", LMUtils
									.getUserLogin(request)));
		} catch (Exception e) {
			ErrorPage.printErrorPage(e.getMessage(), request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
